package com.jorkyin.hfmusic.ui.main;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.jorkyin.hfmusic.R;

import java.util.ArrayList;

public class MusicListAdapter extends RecyclerView.Adapter<MusicListAdapter.MyViewHolder> {
    private Context mContext;
    private ArrayList<Music> mMusics = new ArrayList<>();
    private static final String TAG="MusicListAdapter";

    MusicListAdapter(Context context) {
        this.mContext = context;
    }

    void addMusic(Music music) {
        mMusics.add(0,music);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_music,parent,false);
        MyViewHolder holder= new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        Music music = mMusics.get(position);
        Log.i(TAG, "onBindViewHolder: name="+music.getName()+" writer="+music.getWriter());
        holder.name.setText(music.getName());
        holder.writer.setText(music.getWriter());

    }

    @Override
    public int getItemCount() {
        return mMusics==null?0:mMusics.size();
    }


    class MyViewHolder extends RecyclerView.ViewHolder {
        private TextView name;
        private TextView writer;
        MyViewHolder(@NonNull View view) {
            super(view);
            name=view.findViewById(R.id.music_name);
            writer=view.findViewById(R.id.music_writer);
        }
    }
}
